package com.djl.sqtg.util;

import com.djl.sqtg.entity.Goods;
import com.djl.sqtg.mapper.GoodsMapper;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.model.JDBCDataModel;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
import org.apache.mahout.cf.taste.similarity.precompute.example.GroupLensDataModel;
import org.springframework.beans.factory.annotation.Autowired;

import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ItemCFUtil {
    // 1.准备数据
    public DataModel getMySQLDataModel() {

        /*  文件获取数据源
               File file = new File("D:\\clicks.dat");
                DataModel dataModel;
                try {
                    dataModel = new GroupLensDataModel(file);
                    return dataModel;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
        */
            MysqlDataSource dataSource=new MysqlDataSource();
            dataSource.setServerName("localhost");
            dataSource.setUser("root");
            dataSource.setPassword("123456");
            dataSource.setDatabaseName("cgpdb");//数据库名字
            dataSource.setURL(dataSource.getURL() + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&autoReconnect=true&useSSL=false");
        //参数1：mysql数据源信息，参数2：表名，参数3：用户列字段，参数4：商品列字段，参数5：偏好值字段，参数6：时间戳
//            JDBCDataModel dataModel=new MySQLJDBCDataModel(dataSource,"itemcf","uid","gid","browse", "time");
            JDBCDataModel dataModel=new MySQLJDBCDataModel(dataSource,"review","uid","gid","rating", "reviewtime");
            return dataModel;
    }

//         @Autowired
//         private GoodsMapper goodsMapper;

        public List getRecommendItemsByItem(Long userId, Long itemId, int howMany) {
//    public void getRecommendItemsByItem(Long userId, Long itemId, int howMany) {


            // 2.计算相似度，相似度算法有很多种，采用基于皮尔逊相关性的相似度
            ItemSimilarity itemSimilarity = null;
            try {

                itemSimilarity = new PearsonCorrelationSimilarity(getMySQLDataModel());
                System.out.println( getMySQLDataModel().getItemIDs());
//                itemSimilarity.itemSimilarities(itemId,otherItemIdsList);
                System.out.println("商品"+itemId+"与其他商品的皮尔逊相似度计算如下：");
                System.out.println("商品1--------->  "+itemSimilarity.itemSimilarity(itemId,1));
                System.out.println("商品2--------->  "+itemSimilarity.itemSimilarity(itemId,2));
                System.out.println("商品3--------->  "+itemSimilarity.itemSimilarity(itemId,3));
                System.out.println("商品4--------->  "+itemSimilarity.itemSimilarity(itemId,4));
                System.out.println("商品5--------->  "+itemSimilarity.itemSimilarity(itemId,5));
                System.out.println("商品6--------->  "+itemSimilarity.itemSimilarity(itemId,6));
                System.out.println("商品7--------->  "+itemSimilarity.itemSimilarity(itemId,7));
            } catch (TasteException e) {
                e.printStackTrace();
                System.out.println("没有计算相似度");
            }

            // 3.构建推荐器，使用基于物品的协同过滤推荐
            System.out.println("使用基于物品的协同过滤算法");
            System.out.println("--------------------------------------");
            GenericItemBasedRecommender recommender = new GenericItemBasedRecommender(getMySQLDataModel(), itemSimilarity);
            long start = System.currentTimeMillis();
            // 4.计算用户userId当前浏览的商品itemId，推荐howMany个相似的商品，recommendedItemList为推荐商品列表
            List<RecommendedItem> recommendedItemList = null;


            //4-1 使用 recommender.recommendedBecause
            System.out.println("使用 recommender.recommendedBecause");
            try { //给用户ID等于5的用户推荐10个与商品编号为2398相似的商品
//                recommendedItemList = recommender.recommendedBecause(5, 2398, 10);
                recommendedItemList = recommender.recommendedBecause(userId, itemId, howMany);
            } catch (TasteException e) {
                e.printStackTrace();
            }
            System.out.println("使用recommender.recommendedBecause(userId, itemId, howMany);构建推荐器");
            System.out.println("根据用户"+userId+"当前浏览的商品"+itemId+"，推荐"+howMany+"个相似的商品");
            if (recommendedItemList.isEmpty()){
                System.out.println("==========没有与该商品相似的商品=========");
                return null;
            }else{
                //打印推荐的结果
                List<Long> itemIds = new ArrayList<Long>();
                for (RecommendedItem recommendedItem : recommendedItemList) {
                    System.out.println(recommendedItem);
                    itemIds.add(recommendedItem.getItemID());
                }
                System.out.println("推荐出来的商品id集合" + itemIds);
                return itemIds;
            }


            //4-2 使用 recommender.mostSimilarItems
/*            System.out.println("-----------------------------------------------");
            System.out.println("使用 recommender.mostSimilarItems");
            List<RecommendedItem> recommendedItemList1 = null;
            try {
                recommendedItemList1 = recommender.mostSimilarItems(itemId, howMany);
            } catch (TasteException e) {
                e.printStackTrace();
            }
            System.out.println("使用recommender.mostSimilarItems(itemId, howMany);构建推荐器");
            System.out.println("根据用户"+userId+"当前浏览的商品"+itemId+"，推荐"+howMany+"个相似的商品");
            //打印推荐的结果
            List itemIds1 = new ArrayList();
            for (RecommendedItem recommendedItem : recommendedItemList1) {
                System.out.println(recommendedItem);
                itemIds1.add(recommendedItem.getItemID());
            }
            System.out.println("推荐出来的商品id集合" + itemIds1);*/

//            List<Goods> list = goodsMapper.getAllGoodsByIds(itemIds);
            //根据商品id查询商品
//                if (itemIds != null && itemIds.size() > 0) {
//                    list = goodsMapper.getAllGoodsByIds(itemIds);
//                    System.out.println("最终结果===="+list.toString());
//                } else {
//                    System.out.println("------没有推荐商品------");
//                }
//                System.out.println("推荐数量:" + list.size() + "    耗时：" + (System.currentTimeMillis() - start));
//            return itemIds1;



    }
}